<template>
  <el-row :gutter="20">
    <el-col :span="24">
      <el-form
        v-if="showCenterDeptF"
        class="head pt20"
        ref="centerDeptForm"
        :model="centerDeptForm"
        label-width="40px"
        style="width: 92%;"
      >
        <el-row type="flex" justify="end" class="filter-box">
          <el-col :span="5">
            <el-form-item label="时间">
              <div class="block time-filter">
                <el-date-picker
                  v-model="centerDeptForm.time"
                  type="daterange"
                  align="left"
                  unlink-panels
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :picker-options="pickerOptions2"
                  value-format="yyyy-MM-dd"
                  size="mini"
                  @change="handleCenterDept"
                ></el-date-picker>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="4" v-if="promotionList">
            <el-form-item label="渠道">
              <el-select
                v-model="centerDeptForm.promotions"
                multiple
                collapse-tags
                placeholder="筛选渠道"
                @change="handleCenterDept"
              >
                <el-option v-for="item in promotionList" :key="item" :label="item" :value="item"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="类型">
              <el-select
                v-model="centerDeptForm.type"
                placeholder="筛选类型"
                @change="handleCenterDept"
              >
                <el-option label="上门" value="visit"></el-option>
                <el-option label="签单" value="sign"></el-option>
                <el-option label="提车" value="pickCar"></el-option>
                <el-option label="收费" value="charge"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2" class="relative">
            <el-button v-if="!centerDeptFilter" @click="handleShowCD">业务筛选...</el-button>
            <el-card class="box-card" v-else>
              <div slot="header">
                <span>中心部门筛选</span>
                <el-button style="float: right; padding: 3px 0" type="text" @click="handleShowCD">收起</el-button>
              </div>
              <el-form-item label="中心筛选：" label-width="80x">
                <el-checkbox-group v-model="centerDeptForm.centers" @change="handleCenterDept">
                  <el-checkbox v-for="item in centerList" :key="item" :label="item" name="type"></el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <div>
                <el-form-item v-for="(item1, index) in deptList1" :key="index">
                  <el-checkbox-group v-model="centerDeptForm.depts" @change="handleCenterDept">
                    <el-checkbox
                      v-for="(item, index) in item1"
                      :key="index"
                      :label="item.name"
                      name="type"
                    ></el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </el-form>
      <div class="filter-btn pointer" @click="handleSwitchCenterDeptF">
        <i class="fa fa-filter fa-2x filter" aria-hidden="true" style="color: #409EFF;"></i>
        <span style="color: #409EFF;" class="fs16">过滤</span>
      </div>
      <div class="grid-content-center bg-purple pt20 pl15" id="center-dept"></div>
    </el-col>
  </el-row>
</template>

<script>
import { option7, pickerOptions2 } from "@/../config/Stats_options";
import { getMonthStartDate, getMonthEndDate } from "@/utils/getCurrMonth";

export default {
  name: "bar",
  data() {
    return {
      centerDeptForm: {
        time: [getMonthStartDate(), getMonthEndDate()],
        promotions: [],
        type: "visit",
        centers: [],
        depts: []
      },
      showCenterDeptF: false,
      pickerOptions2,
      centerDeptFilter: false
    };
  },
  watch: {
    promotionList() {
      this.centerDeptForm.promotions = this.promotionList;
    },
    centerList() {
      this.centerDeptForm.centers = this.centerList;
    },
    deptList1() {
      this.centerDeptForm.depts = this.depts1;
    }
  },
  props: ["promotionList", "centerList", "deptList1", "depts1"],
  methods: {
    handleSwitchCenterDeptF() {
      this.showCenterDeptF = !this.showCenterDeptF;
    },
    handleCenterDept() {
      option7.xAxis[0].data = [];
      option7.series[0].data = [];
      this.$axios
        .post("/api/statistics/Stats_CenterDept", {
          from: this.centerDeptForm.time[0],
          to: this.centerDeptForm.time[1],
          promotions: this.centerDeptForm.promotions,
          type: this.centerDeptForm.type,
          centers: this.centerDeptForm.centers,
          depts: this.centerDeptForm.depts
        })
        .then(data => {
          option7.xAxis[0].data = data.xAxis;
          option7.series[0].data = data.resoult;
          option7.series[0].name = data.name;
        })
        .then(() => {
          this.updateCenterDept();
        });
    },
    handleShowCD() {
      this.centerDeptFilter = !this.centerDeptFilter;
    },
    updateCenterDept() {
      let centerDept = this.$echarts.init(
        document.getElementById("center-dept")
      );
      centerDept.setOption(option7, true);
    }
  }
};
</script>
<style>
</style>